 package net.ruixin.web.struts2.plugins;

 import com.google.gson.Gson;
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.util.ValueStack;
 import java.io.PrintWriter;
 import javax.servlet.http.HttpServletResponse;
 import org.apache.struts2.dispatcher.StrutsResultSupport;

 public class JsonResultType extends StrutsResultSupport
 {
   private static final long serialVersionUID = 1L;
   private String json = "";
   private String data = "";

   public JsonResultType() {
   }

   public JsonResultType(String location) {
     super(location);
   }

   protected void doExecute(String finalLocation, ActionInvocation invocation)
     throws Exception
   {
     HttpServletResponse httpResponse = (HttpServletResponse)invocation.getInvocationContext().get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
     httpResponse.setHeader("Pragma", "No-cache");
     httpResponse.setHeader("Cache-Control", "no-cache");
     httpResponse.setDateHeader("Expires", 0L);
     String contentType = "text/json; charset=UTF-8";
     httpResponse.setContentType(contentType);

     PrintWriter writer = httpResponse.getWriter();

     String jsonData = conditionalParse(this.json, invocation);
     if (!this.data.isEmpty()) {
       Gson gs = new Gson();
       Object dataObject = invocation.getStack().findValue(this.data);
       jsonData = gs.toJson(dataObject);
     }

     writer.println(jsonData);
     writer.flush();
     writer.close();
   }

   public String getJson() {
     return this.json;
   }

   public void setJson(String json) {
     this.json = json;
   }

   public String getData() {
     return this.data;
   }

   public void setData(String data) {
     this.data = data;
   }
 }
